package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.cart.client.CartFeignClient;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class CartController {
    @Autowired
    private CartFeignClient cartFeignClient;
    @Autowired
    private ProductFeignClient productFeignClient;

    //  添加购物车控制器：
    //  window.location.href = 'http://cart.gmall.com/addCart.html?skuId=' + this.skuId + '&skuNum=' + this.skuNum
    @GetMapping("addCart.html")
    public String addCart(HttpServletRequest request){
        String skuId = request.getParameter("skuId");
        String skuNum = request.getParameter("skuNum");
        //  调用添加购物车方法  前端发送异步请求执行了
        //  cartFeignClient.addToCart(Long.parseLong(skuId), Integer.parseInt(skuNum));
        SkuInfo skuInfo = productFeignClient.getSkuInfo(Long.parseLong(skuId));
        request.setAttribute("skuNum",skuNum);
        request.setAttribute("skuInfo",skuInfo);
        //  返回页面
        return "cart/addCart";
    }

    //  购物车列表页面！
    @GetMapping("cart.html")
    public String cartList(){

        return "cart/index";
    }

}
